package com.tg.appcommon.android;

import android.app.Activity;
import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.text.TextUtils;
import android.text.method.ScrollingMovementMethod;
import android.view.Display;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.WindowManager;
import android.widget.FrameLayout;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.annotation.ColorInt;
import androidx.annotation.LayoutRes;

import com.module.appcommon.R;

public class TGAlertDialog {
    private Context mContext;
    private Dialog mDialog;
    private TextView mTitleTextView;
    private TextView mMessageTextView;
    private TextView mNegButton;
    private TextView mPosButton;
    private Display display;
    private boolean isShowTitle = false;
    private boolean isShowMessage = false;
    private boolean isShowPosButton = false;
    private boolean isShowNegButton = false;
    private boolean isShowBottomDialog = false;

    public static TGAlertDialog openLampDialog(final Context context, final View.OnClickListener positiveBtnListener){
        if (context != null){
            TGAlertDialog alertDialog = new TGAlertDialog(context).builder(R.layout.dialog_custom_lamp).setTitle(R.string.dialog_lamp_device_bottom_desc).setMessage(R.string.dialog_lamp_device_mid_desc, null).setPositiveButton(positiveBtnListener);
            alertDialog.show(ResourcesUtil.getResources().getColor(R.color.bottom_sheet_text_color));
            return alertDialog;
        }
        return null;
    }

    public TGAlertDialog(Context context) {
        this.mContext = context;
        WindowManager windowManager = (WindowManager) context.getApplicationContext()
                .getSystemService(Context.WINDOW_SERVICE);
        display = windowManager.getDefaultDisplay();
    }

    public TGAlertDialog builder(@LayoutRes int resource, boolean isShowBottomDialog) {
        View view = LayoutInflater.from(mContext).inflate(resource, null);

        LinearLayout mLLayout = view.findViewById(R.id.lLayout_bg);
        mTitleTextView = view.findViewById(R.id.txt_title);
        mTitleTextView.setVisibility(View.GONE);
        mMessageTextView = view.findViewById(R.id.txt_msg);
        mMessageTextView.setVisibility(View.GONE);
        mNegButton = view.findViewById(R.id.btn_neg);
        mNegButton.setVisibility(View.GONE);
        mPosButton = view.findViewById(R.id.btn_pos);
        mPosButton.setVisibility(View.GONE);
        this.isShowBottomDialog = isShowBottomDialog;
        float x = 0.8f;
        if (isShowBottomDialog){
            mDialog = new CruiseBottomDialog(mContext, R.style.alert_dialog);
            x = 0.95f;
        }else{
            mDialog = new Dialog(mContext, R.style.alert_dialog);
        }

        mDialog.setContentView(view);
        mDialog.setCanceledOnTouchOutside(false);
        mMessageTextView.setMovementMethod(ScrollingMovementMethod.getInstance());

        mLLayout.setLayoutParams(new FrameLayout.LayoutParams((int) (display
                .getWidth() * x), LinearLayout.LayoutParams.WRAP_CONTENT));
        return this;
    }

    public TGAlertDialog builder(@LayoutRes int resource) {
        return builder(resource, false);
    }

    public TGAlertDialog builder() {
        return builder(R.layout.dialog_custom);
    }

    public void setCanceledOnTouchOutside(boolean canceled) {
        if (mDialog != null) {
            mDialog.setCanceledOnTouchOutside(canceled);
        }
    }

    public void setOnDismissListener(DialogInterface.OnDismissListener onDismissListener) {
        if (mDialog != null) {
            mDialog.setOnDismissListener(onDismissListener);
        }
    }

    public void setBackPressed() {
        mDialog.setOnKeyListener(new DialogInterface.OnKeyListener() {

            @Override
            public boolean onKey(DialogInterface dialogInterface, int keyCode, KeyEvent keyEvent) {
                if (keyCode == KeyEvent.KEYCODE_BACK && keyEvent.getRepeatCount() == 0) {
                    return true;
                }
                return false;
            }
        });
    }

    public TGAlertDialog setPosButtonColor(int color) {
        mPosButton.setTextColor(ResourcesUtil.getResources().getColor(color));
        return this;
    }

    public TGAlertDialog setTitle(String title) {
        isShowTitle = true;
        if (!TextUtils.isEmpty(title)&&mTitleTextView!=null) {
            mTitleTextView.setText(title);
        }
        return this;
    }


    public TGAlertDialog setTitle(int resId) {
        if (resId == 0){
            return this;
        }
        return setTitle(mContext.getResources().getString(resId));
    }

    public TGAlertDialog setMessage(String message) {
        isShowMessage = true;
        if (message == null) {
            isShowMessage = false;
            mMessageTextView.setVisibility(View.GONE);
        } else if ("".equals(message)) {
            mMessageTextView.setText("内容");
        } else {
            mMessageTextView.setText(message);
        }
        return this;
    }

    public TGAlertDialog setMessage(int resId, final View.OnClickListener listener) {
        return setMessage(mContext.getResources().getString(resId), listener);
    }


    public TGAlertDialog setTitle(String title, final View.OnClickListener listener) {
        isShowTitle = true;
        if (!TextUtils.isEmpty(title)&&mTitleTextView!=null) {
            mTitleTextView.setText(title);
        }

        mTitleTextView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (listener != null){
                    cancel();
                    listener.onClick(view);
                }
            }
        });
        return this;
    }



    public TGAlertDialog setMessage(String message, final View.OnClickListener listener) {
        isShowMessage = true;
        if (message == null) {
            isShowMessage = false;
            mMessageTextView.setVisibility(View.GONE);
        } else if ("".equals(message)) {
            mMessageTextView.setText("内容");
        } else {
            mMessageTextView.setText(message);
        }
        mMessageTextView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                cancel();
                if (listener != null){
                    listener.onClick(view);
                }
            }
        });
        return this;
    }


    public TGAlertDialog setMessage(CharSequence message) {
        isShowMessage = true;
        if (message == null) {
            isShowMessage = false;
            mMessageTextView.setVisibility(View.GONE);
        } else if ("".equals(message)) {
            mMessageTextView.setText("内容");
        } else {
            mMessageTextView.setText(message);
        }
        return this;
    }

    public TGAlertDialog setMessage(int resId) {
        return setMessage(mContext.getResources().getString(resId));
    }

    public TGAlertDialog setCancelable(boolean cancel) {
        mDialog.setCancelable(cancel);
        return this;
    }

    public TGAlertDialog setPositiveButton(String text, final View.OnClickListener listener) {
        isShowPosButton = true;
        if ("".equals(text)) {
            mPosButton.setText("确定");
        } else {
            mPosButton.setText(text);
        }
        mPosButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                cancel();
                if (listener != null) {
                    listener.onClick(v);
                }
            }
        });
        return this;
    }

    public TGAlertDialog setPositiveButton(final View.OnClickListener listener) {
        isShowPosButton = true;
        mPosButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                cancel();
                if (listener != null) {
                    listener.onClick(v);
                }
            }
        });
        return this;
    }

    public TGAlertDialog setPositiveButton(int resId, final View.OnClickListener listener) {
        if (resId == 0){
            return this;
        }
        return setPositiveButton(mContext.getResources().getString(resId), listener);
    }

    public TGAlertDialog setNegativeButton(int resId, final View.OnClickListener listener) {
        if (resId == 0){
            return this;
        }
        return setNegativeButton(mContext.getResources().getString(resId), listener);
    }

    public TGAlertDialog setNegativeButton(String text, final View.OnClickListener listener) {
        isShowNegButton = true;
        if ("".equals(text)) {
            mNegButton.setText("取消");
        } else {
            mNegButton.setText(text);
        }
        mNegButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (listener != null) {
                    listener.onClick(v);
                }
                cancel();
            }
        });
        return this;
    }

    public TextView getMessageTextView() {
        return mMessageTextView;
    }

    private void setLayout() {
        if (!isShowTitle && !isShowMessage) {
            mTitleTextView.setText("提示");
            mTitleTextView.setVisibility(View.GONE);
        }

        if (isShowTitle) {
            mTitleTextView.setVisibility(View.VISIBLE);
        }

        if (isShowMessage) {
            mMessageTextView.setVisibility(View.VISIBLE);
        }

        if (!isShowPosButton && !isShowNegButton) {
            mPosButton.setText("确定");
            mPosButton.setVisibility(View.VISIBLE);
            mPosButton.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    cancel();
                }
            });
        }

        if (isShowPosButton && isShowNegButton) {
            mPosButton.setVisibility(View.VISIBLE);
            mNegButton.setVisibility(View.VISIBLE);
        }

        if (isShowPosButton && !isShowNegButton) {
            mPosButton.setVisibility(View.VISIBLE);
        }

        if (!isShowPosButton && isShowNegButton) {
            mNegButton.setVisibility(View.VISIBLE);
        }
    }

    public void show() {
        show(ResourcesUtil.getResources().getColor(R.color.black));
    }

    public void show(@ColorInt int color) {
        setLayout();
        mTitleTextView.setTextColor(color);

        if (mContext instanceof Activity) {
            if (((Activity) mContext).isFinishing()) return;
            if (((Activity) mContext).isDestroyed()) return;
        } else {
            // 需要使用Activity作为Context
            return;
        }

        mDialog.show();
    }

    public boolean isShowing() {
        return mDialog != null && mDialog.isShowing();
    }

    public void dismiss() {
        if (mDialog != null) {
            mDialog.dismiss();
        }
    }

    public void cancel() {
        if (mDialog.isShowing() || mDialog != null) {
            if (((Activity) mContext).isFinishing()) return;
            mDialog.cancel();
        }
    }

}
